package com.hsy.baoinbao.modules.main.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.main.ui.find.FindPhoneActivity;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/18.
 */
public class GoodsIntoFirstActivity extends CommonBaseActivityWithSimpleActionBar {
    private EditText phone_edit;
    private TextView finish;
    private Button select_text;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            CommonUtils.hideProgressDialog();
            try {
                JSONObject result = new JSONObject(response);
                Intent intent = new Intent(GoodsIntoFirstActivity.this, GoodsIntoSecondActivity.class);
                intent.putExtra("name_merchant", result.optString("name_merchant"));
                intent.putExtra("name", result.optString("name"));
                intent.putExtra("user_name", result.optString("user_name"));
                intent.putExtra("address", result.optString("address"));
                intent.putExtra("uid", result.optString("id"));
                startActivity(intent);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_into);
        setTitle(R.string.title_user_find);
        phone_edit = F(R.id.phone_edit);
        finish = F(R.id.finish);
        select_text = F(R.id.select_text);
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(phone_edit.getText().toString())) {
                    showToast(R.string.base_add_phone);
                } else {
                    CommonUtils.showProgressDialog(GoodsIntoFirstActivity.this);
                    Map<String, String> data = new HashMap<>();
                    data.put("token", CommonUtils.getToken(GoodsIntoFirstActivity.this));
                    data.put("user_name", phone_edit.getText().toString());
                    HttpUtil.httpPostWithParams(GoodsIntoFirstActivity.this, 0, Consts.API_MALL_SEARCH_MERCHANT, data, callBack);

                }
            }
        });
        select_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GoodsIntoFirstActivity.this, FindPhoneActivity.class);
                intent.putExtra("type","goods");
                startActivityForResult(intent, 1918);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        DPLogUtil.d("onActivityResult");
        if (1918 == requestCode && 1919 == resultCode) {
            if (null == data) {
                DPLogUtil.d("null==data");
                return;
            }
            String phone = data.getStringExtra("phone984988");
            DPLogUtil.d("null!=data" + phone);
            phone_edit.setText(phone);
        }
    }
}
